package com.kfm.src;

import java.sql.*;

public class demo
{
    public static void main(String[] args) throws SQLException {

        Statement statement = startDriver();
        //C(statement, 6, "法务部");
        //System.out.println(b);
        R(statement);
        // 执行完CRUD需要进行关闭操作
        isclose(statement);
    }
    public static void isclose(Statement statement)
    {
        try {
            if (statement.isClosed())
            {
                System.out.println("关闭");
            }
            else
            {
                statement.close();
                System.out.println("关闭");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public <T extends Object> boolean U(Statement statement,T olda,T newa) {
        String str = "update dept_new set deptname = '%s' where deptname = '%s' ".formatted(newa,olda);
        long l;
        try {
            l = statement.executeLargeUpdate(str);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return l>0?true:false;
    }


    private static void R(Statement statement) {
        /*
         * 默认查询所有
         */
        String str = "SELECT * FROM dept_new";
        try {
            ResultSet resultSet = statement.executeQuery(str);
            while (resultSet.next())
            {
                int column = resultSet.getInt(1);
                String deptname = resultSet.getString("deptname");
                System.out.println("部门号为：" + column+ "    " + "部门名称:" + deptname);
            }
            System.out.println("查询结束");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    private static boolean C(Statement statement,int deptno,String deptname) {
        String s = "insert into dept_new (deptno,deptname) Values(%d,'%s')".formatted(deptno,deptname);
        long l;
        try {
            l = statement.executeLargeUpdate(s);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return l>0?true:false;
    }
    private static boolean D(Statement statement, String str) {
        String s = "delete from dept_new where deptname = '%s'".formatted(str);
        long l;
        try {
            l = statement.executeLargeUpdate(s);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return l>0?true:false;
    }






    private static Statement startDriver() {
        // 加载驱动类， 解耦
//            DriverManager.registerDriver(new Driver());
        // 全限定类名
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        // 获取连接
        String url = "jdbc:mysql://localhost:3306/company_info";
        String user = "root";
        String pass = "";
       Statement sta;
        try
        {
            Connection connection = DriverManager.getConnection(url, user, pass);
            sta = connection.createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return sta;

    }
}
